/**
 * 
 */
package hu.radai.jsfportal.filter;

import hu.radai.jsfportal.log.LogManagerUtil;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Radai Tamas
 */
public class SecurityFilterImpl implements SecurityFilter {

    /**
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public void init(final FilterConfig filterConfig) throws ServletException {
        LogManagerUtil.debug(SecurityFilterImpl.class, "security filter is initialized");
    }

    /**
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
        LogManagerUtil.debug(SecurityFilterImpl.class, "security filter is destroyed");
    }

    /**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(final ServletRequest servletRequest,
            final ServletResponse servletResponse,
            final FilterChain filterChain) throws IOException,
            ServletException {
        try {
            final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
            final HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

            final String request = httpServletRequest.getServletPath();
            final String[] requestArray = request.split("/");

            int i = 0;
            for (final String string : requestArray) {
            	LogManagerUtil.debug(SecurityFilterImpl.class, "requested elements: " + i++ + " " + string);
            }

            if (requestArray.length > 0) {
                // inside request
                LogManagerUtil.debug(SecurityFilterImpl.class, "inside request");
                httpServletRequest.getSession().setAttribute("brumbrumm", "mackosajt");
                if (checkAuthentication(httpServletRequest, httpServletResponse)) {
                    httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/index.xhtml");
                }
            } else {
                // outside request
                LogManagerUtil.debug(SecurityFilterImpl.class, "outside request");
            }
        } catch (final Throwable t) {
            t.printStackTrace();
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    /**
     * @see SecurityFilter#checkAuthentication(java.lang.Object,
     *      java.lang.Object)
     */
    @Override
    // TODO(Radai Tamas): parameterek tiszazasa
    public Boolean checkAuthentication(final Object o1, final Object o2) {
        Boolean isLoggedIn = false;

        try {
            final HttpServletRequest httpServletRequest = (HttpServletRequest) o1;
            final HttpSession httpSession = httpServletRequest.getSession();
            final Object loggedIn = httpSession.getAttribute("LoggedIn");
            if (loggedIn != null) {
                // TODO(Radai Tamas): check loggedIn value
                isLoggedIn = true;
            }

            final Enumeration<String> sessionAttributeNames = httpSession.getAttributeNames();
            while (sessionAttributeNames.hasMoreElements()) {
                final String sessionAttributeName = sessionAttributeNames.nextElement();
                final Object sessionAttributeValue = httpSession.getAttribute(sessionAttributeName);
                LogManagerUtil.debug(SecurityFilterImpl.class, "ses attr = " + sessionAttributeName + " : " + sessionAttributeValue);
            }

            final Enumeration<String> requestAttributeNames = httpServletRequest.getAttributeNames();
            while (requestAttributeNames.hasMoreElements()) {
                final String requestAttributeName = requestAttributeNames.nextElement();
                final Object requestAttributeValue = httpServletRequest.getAttribute(requestAttributeName);
                LogManagerUtil.debug(SecurityFilterImpl.class, "req attr = " + requestAttributeName + " : " + requestAttributeValue);
            }
        } catch (final Throwable t) {
            t.printStackTrace();
        }

        return isLoggedIn;
    }
}
